package com.li.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;

/**
 * Author: Li
 * Date: 2017/12/13 0013
 * Description: 权限相关操作
 */

public class PermissionUtil {
    /**
     * 检查是否拥有某个权限
     * @param context   context
     * @param permission    权限
     * @return  是否拥有权限
     */
    public static boolean checkPermission(Context context, String permission) {
        return ActivityCompat.checkSelfPermission(context, permission)
                == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 检查是否拥有某些权限
     * @param context   context
     * @param permissions   权限集合
     * @return  有一项权限未拥有返回false，都拥有时返回true
     */
    public static boolean checkPermissions(Context context,String[] permissions){
        for (String permission : permissions) {
            if (!checkPermission(context,permission)) {
                return false;
            }
        }
        return true;
    }
    public static void requestPermissions(Activity activity, String[] permissions, int requestCode){
        ActivityCompat.requestPermissions(activity,permissions,requestCode);
    }
}
